<template>
  <div class='articles-container'>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="22">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="关键字" style="margin:0 0 0 20px">
              <el-input size="mini" v-model="requestParams.keyword"></el-input>
            </el-form-item>
            <el-form-item label="状态" style="margin:0 0 0 50px">
              <el-select placeholder="请选择" size="mini" v-model="requestParams.state">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin:0 0 0 10px">
              <el-button size="mini" @click="clear()">清除</el-button>
              <el-button type="primary" size="mini" @click="filter()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button type="success" icon="el-icon-edit" size="mini" style="margin:0 10px 0 0" @click="openDialog()">新增技巧</el-button>
        </el-col>
      </el-row>
      <!-- 总条数信息 -->
      <el-alert
        :title="title"
        type="info"
        :closable="false"
        style="margin:20px 0 20px 0"
        show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table
        :data="articles"
        style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="100"
          :index="indexMethod">
        </el-table-column>
        <el-table-column
          label="文章标题"
          width="400"
          >
          <template #default="{row}">{{row.title}}  <a href="#" :class="row.videoURL?'el-icon-film':''" @click.prevent="playVideo(row.videoURL)"></a></template>
        </el-table-column>
        <el-table-column
          prop="visits"
          label="阅读数"
          >
        </el-table-column>
        <el-table-column
          prop="username"
          label="录入人">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="录入时间">
        </el-table-column>
        <el-table-column
          label="状态"
          width="80">
          <template #default="{row}">{{row.state===1?'已启用':'已禁用'}}</template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template #default="{row}">
            <el-link type="primary" :underline="false" style="margin:0 10px 0 0" @click="previewArticle(row)">预览</el-link>
            <el-link type="primary" :underline="false" style="margin:0 10px 0 0" @click="toggleState(row)">{{row.state===1?'启用':'禁用'}}</el-link>
            <el-link :type="row.state===1?'primary':'info'" :underline="false" style="margin:0 10px 0 0" @click="editArticle(row)" :disabled="row.state===0">修改</el-link>
            <el-link :type="row.state===1?'primary':'info'" :underline="false" @click="delArticle(row.id)" :disabled="row.state===0">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin:20px 0 0 0"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="requestParams.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="requestParams.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 预览页面组件 -->
    <articles-preview
    :previewdialog.sync="previewdialog"
    :currArticle="currArticle"></articles-preview>
    <!-- 添加页面/修改页面 -->
    <articles-add
    :addDialog.sync="addDialog"
    @updateList="updateList"
    :addtitle="addtitle"
    :currArticle="currArticle"
    ref="articlesEdit"></articles-add>
    <!-- 视频播放页面 -->
    <div class="VideoBox" v-if="videoURL">
      <div class="close" @click="Close()"><span class="el-icon-close"></span></div>
      <div class="playVideo">
        <video :src="videoURL" ref="video"></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/articles.js'
import articlesPreview from '../components/articles-preview.vue'
import ArticlesAdd from '../components/articles-add.vue'
export default {
  components: { articlesPreview, ArticlesAdd },
  data () {
    return {
      requestParams: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      articles: [],
      total: 0,
      title: '',
      previewdialog: false,
      addDialog: false,
      currArticle: {},
      addtitle: '',
      videoURL: null
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    // 列表渲染
    async getlist () {
      const { data } = await list(this.requestParams)
      this.articles = data.items
      this.total = data.counts
      this.title = `数据一共有${data.counts}条`
    },
    // 分页器点击事件
    handleSizeChange (val) {
      this.requestParams.pagesize = val
      this.getlist()
    },
    // 分页器点击事件
    handleCurrentChange (val) {
      this.requestParams.page = val
      this.getlist()
    },
    // 处理表格索引事件
    indexMethod (index) {
      return index + 1 + (this.requestParams.page - 1) * this.requestParams.pagesize
    },
    // 清除输入框内容
    clear () {
      this.requestParams.keyword = null
      this.requestParams.state = null
    },
    // 搜索特定的数据
    filter () {
      this.getlist()
    },
    // 预览页面
    previewArticle (row) {
      this.previewdialog = true
      this.currArticle = row
    },
    // 添加页面
    openDialog () {
      this.addtitle = '新增文章'
      this.addDialog = true
    },
    // 添加技巧事件
    updateList () {
      this.addDialog = false
      this.getlist()
    },
    // 修改页面
    editArticle (row) {
      this.addtitle = '修改文章'
      this.addDialog = true
      this.currArticle = row
      this.$nextTick(() => {
        this.$refs.articlesEdit.open()
      })
    },
    // 删除
    delArticle (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        this.getlist()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 切换按钮
    toggleState (row) {
      if (row.state === 1) {
        row.state = 0
      } else if (row.state === 0) {
        row.state = 1
      }
    },
    // 播放视频
    playVideo (Url) {
      this.videoURL = Url
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 关闭视频
    Close () {
      this.$nextTick(() => {
        this.$refs.video.pause()
      })
      this.videoURL = null
    }
  }
}
</script>

<style scoped lang='scss'>
.articles-container{
  margin: 20px 20px;
}
.el-icon-film{
  color: blue;
}
.VideoBox{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9999;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.4);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .playVideo{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
